package com.frost.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
@Aspect
public class MyAspectDemo {
    // 使⽤ @annotation 切点表达式定义切点,只对 @TimeRecord⽣效
    // 也可以说是@TimeRecord注解功能的实现
    @Around("@annotation(com.frost.aop.aspect.TimeRecord)")
    public Object timeRecord(ProceedingJoinPoint joinPoint) {
        long start = System.currentTimeMillis();
        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Throwable e) {
            log.info(joinPoint.toString() + "发行异常: " + e);
        }
        log.info(joinPoint.toString() + "消耗时间: " + (System.currentTimeMillis() - start) + "ms");
        return result;
    }

//    // 根据注解匹配,作用在注解 @RequestMapping上
//    @After("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
//    public void doAfter() {
//        log.info("执行MyAspectDemo.doAfter方法...");
//    }

}
